package com.ruoyi.safe.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.safe.domain.CommonCooperator;
import com.ruoyi.safe.domain.CommonCooperatorBadness;
import com.ruoyi.safe.mapper.CommonCooperatorBadnessMapper;
import com.ruoyi.safe.mapper.CommonCooperatorMapper;
import com.ruoyi.safe.service.ICommonCooperatorService;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 合作单位Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-08
 */
@Service
public class CommonCooperatorServiceImpl implements ICommonCooperatorService 
{
    @Autowired
    private CommonCooperatorMapper commonCooperatorMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private CommonCooperatorBadnessMapper commonCooperatorBadnessMapper;

    /**
     * 查询合作单位
     * 
     * @param id 合作单位主键
     * @return 合作单位
     */
    @Override
    public CommonCooperator selectCommonCooperatorById(Long id)
    {
        return commonCooperatorMapper.selectCommonCooperatorById(id);
    }

    /**
     * 查询合作单位列表
     * 
     * @param commonCooperator 合作单位
     * @return 合作单位
     */
    @Override
    public List<CommonCooperator> selectCommonCooperatorList(CommonCooperator commonCooperator)
    {
        List<CommonCooperator> resultList = new ArrayList<>();
        List<CommonCooperator> commonCooperatorList = commonCooperatorMapper.selectCommonCooperatorList(commonCooperator);
        //遍历合作单位数据
        for (CommonCooperator common : commonCooperatorList) {
            //查询不良记录表中关联数据
            List<CommonCooperatorBadness> commonCooperatorBadnessList = commonCooperatorBadnessMapper.selectCommomnByCooperatorId(common.getId());
            common.setBadnessRecord(commonCooperatorBadnessList.size());
            resultList.add(common);
        }
        return resultList;
    }

    /**
     * 新增合作单位
     * 
     * @param commonCooperator 合作单位
     * @return 结果
     */
    @Override
    public int insertCommonCooperator(CommonCooperator commonCooperator)
    {
        SysDept sysDept = deptMapper.selectDeptById(commonCooperator.getDeptId());
        if (sysDept != null) {
            commonCooperator.setDepartment(sysDept.getDeptName());//所属组织
        }
        commonCooperator.setCreateBy(SecurityUtils.getUsername());
        commonCooperator.setCreateTime(DateUtils.getNowDate());
        return commonCooperatorMapper.insertCommonCooperator(commonCooperator);
    }

    /**
     * 修改合作单位
     * 
     * @param commonCooperator 合作单位
     * @return 结果
     */
    @Override
    public int updateCommonCooperator(CommonCooperator commonCooperator)
    {
        SysDept sysDept = deptMapper.selectDeptById(commonCooperator.getDeptId());
        if (sysDept != null) {
            commonCooperator.setDepartment(sysDept.getDeptName());//所属组织
        }
        commonCooperator.setUpdateTime(DateUtils.getNowDate());
        commonCooperator.setCreateBy(SecurityUtils.getUsername());
        return commonCooperatorMapper.updateCommonCooperator(commonCooperator);
    }

    /**
     * 批量删除合作单位
     * 
     * @param ids 需要删除的合作单位主键
     * @return 结果
     */
    @Override
    public int deleteCommonCooperatorByIds(Long[] ids)
    {
        return commonCooperatorMapper.deleteCommonCooperatorByIds(ids);
    }

    /**
     * 删除合作单位信息
     * 
     * @param id 合作单位主键
     * @return 结果
     */
    @Override
    public int deleteCommonCooperatorById(Long id)
    {
        return commonCooperatorMapper.deleteCommonCooperatorById(id);
    }

    @Override
    public List<CommonCooperator> selectSubPackageList(CommonCooperator commonCooperator) {
        return commonCooperatorMapper.selectSubPackageList();
    }

    @Override
    public List<Map<String,Object>> getSelectSimpleList(CommonCooperator commonCooperator) {
        return commonCooperatorMapper.selectSimpleList(commonCooperator);
    }

    @Override
    public CommonCooperator selectCommonCooperatorByGapsCooperatorId(String gapsCooperatorId) {
        return commonCooperatorMapper.selectCommonCooperatorByGapsCooperatorId(gapsCooperatorId);
    }

    @Override
    public int insertSyncCommonCooperator(CommonCooperator commonCooperator) {
        SysDept sysDept = deptMapper.selectDeptById(commonCooperator.getDeptId());
        if (sysDept != null) {
            commonCooperator.setDepartment(sysDept.getDeptName());//所属组织
        }
        return commonCooperatorMapper.insertCommonCooperator(commonCooperator);
    }

    @Override
    public int updateSyncCommonCooperator(CommonCooperator commonCooperator) {
        SysDept sysDept = deptMapper.selectDeptById(commonCooperator.getDeptId());
        if (sysDept != null) {
            commonCooperator.setDepartment(sysDept.getDeptName());//所属组织
        }
        return commonCooperatorMapper.updateCommonCooperator(commonCooperator);
    }
}
